<template>
	<div class="add_edit">
		<use-box>
			<el-form :model="form" :rules="rules" ref="ruleForm" label-width="100px">
				<el-form-item label="用户名称" prop="member_name" ref="member_name">
					<el-input v-model="form.member_name" placeholder="请输入名称" clearable></el-input>
				</el-form-item>
				<el-form-item label="用户头像">
					<uni-file-picker fileMediatype="image" returnType="object" :image-styles="imageStyle"
						@success="imgUpload" @delete="memberImgDelete" />
				</el-form-item>
				<el-form-item label="订单号" prop="order_id" ref="order_id">
					<el-input v-model="form.order_id" placeholder="请输入订单号" clearable></el-input>
				</el-form-item>
				<el-form-item label="评论星数">
					<el-rate v-model="form.review_cnt" :colors="colors" class="margin_t_10" show-text
						:texts="['差评', '中评', '中评', '好评', '好评']"></el-rate>
				</el-form-item>
				<el-form-item label="评价内容" prop="review_content" ref="review_content">
					<el-input type="textarea" :rows="3" v-model="form.review_content" placeholder="请输入评价内容"></el-input>
				</el-form-item>
				<el-form-item label="晒图">
					<uni-file-picker v-model="evaluateImageValue" fileMediatype="image" returnType="array"
						:image-styles="imageStyle" @delete="evaluateImgDelete" @move="imgMove" />
				</el-form-item>
				<el-form-item label="备注">
					<el-input v-model="form.remark" placeholder="请输入备注" clearable></el-input>
				</el-form-item>
				<el-form-item label="状态">
					<el-switch v-model="form.state" active-color="#ff6a6c" inactive-color="#bbb" active-value="显示"
						inactive-value="隐藏"></el-switch>
				</el-form-item>
				<el-form-item>
					<use-affix position="bottom" :offset="58">
						<el-button :loading="formLoading" round icon="el-icon-check" class="confirm-btn"
							@click="submitForm('ruleForm')">提交</el-button>
						<el-button round icon="el-icon-back" class="return-btn" @click="returnPage">返回</el-button>
					</use-affix>
				</el-form-item>
			</el-form>
		</use-box>
	</div>
</template>

<script>
	const __name = 'wmzmall-goods-comment';
	export default {
		data() {
			return {
				formLoading: false,
				form: {
					goods_id: '',
					member_name: '',
					member_headimg: '',
					order_id: '',
					review_cnt: 0,
					review_content: '',
					review_imgs: '',
					remark: '',
					state: '显示'
				},
				imageStyle: {
					height: '150px',
					width: '150px'
				},
				colors: ['#99A9BF', '#F7BA2A', '#FF9900'],
				evaluateImageValue: [],
				rules: {
					member_name: [{
						required: true,
						message: '请输入用户名称',
						trigger: 'change'
					}],
					order_id: [{
						required: true,
						message: '请输入订单号',
						trigger: 'change'
					}],
					review_content: [{
						required: true,
						message: '请输入评价内容',
						trigger: 'change'
					}]
				}
			};
		},
		methods: {
			submitForm(formName) {
				this.$refs[formName].validate((valid, obj) => {
					// 默认获取第一个未验证 form 属性名
					this.$api.set_unvalidated_form_focus(this, obj);
					if (valid) {
						this.formLoading = true;
						this.form.goods_id = this.$route.query.id;

						if (!this.form.goods_id) {
							this.$message.error('评价商品ID不存在');
							return;
						}

						switch (this.form.review_cnt) {
							case 1:
								this.form.review_type = '差评';
								break;
							case 2:
							case 3:
								this.form.review_type = '中评';
								break;
							case 4:
							case 5:
								this.form.review_type = '好评';
								break;
						}

						if (this.evaluateImageValue.length > 0) {
							this.form.review_imgs = this.evaluateImageValue.map(x => x.url);
						}

						this.$db[__name].add(this.form).then(res => {
							if (res.code == 200) {
								this.$message({
									message: '评价成功',
									type: 'success'
								});
								this.returnPage();
							}
							this.formLoading = false;
						}).catch(() => {
							this.formLoading = false;
						})
					}
				});
			},
			imgUpload(e) {
				this.form.member_headimg = e.tempFilePaths[0];
			},
			memberImgDelete() {
				this.form.member_headimg = '';
			},
			evaluateImgDelete(e) {
				this.evaluateImageValue.splice(this.evaluateImageValue.findIndex(x => x.url == e.tempFilePath), 1);
			},
			imgMove(e) {
				this.evaluateImageValue = e;
			},
			returnPage() {
				uni.$emit('closeTab');
				uni.redirectTo({
					url: '/pages/goods/list/goods_list'
				});
			}
		},
		mounted() {
			this.$nextTick(() => {
				this.$api.scrollOnStorage('.use-box', this.$route.path);
			});

			this.$nextTick(() => {
				this.$api.scrollToStorage('.use-box', this.$route.path);
			});
		}
	};
</script>

<style></style>
